package com.sinapsisempresarial.io;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestSeniat {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, 
    IOException, XPathExpressionException{
		
		StringBuffer contenido = new StringBuffer();
		
		try {
	    	
	    	String nacionalidad = "V";
//	    	String cedula = "14362641";
	    	String cedula = "19733258";
	    	
	        URL url = new URL("http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp?p_cedula=" + cedula);
	        
	        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
	        String line;
	        int i = 0;
	        while ((line = reader.readLine()) != null) {
	        	if(i==73)
	        		contenido.append(line);
	        	i++;
	        }
	        reader.close();

	        System.out.println(contenido.toString());
	        
	    } catch (MalformedURLException e) {

	    } catch (IOException e) {

	    }
	    
	    System.out.println(":"+contenido.indexOf("\">")+":");
	    String contenido1 = contenido.substring(53+2);
	    System.out.println("final:"+contenido1.indexOf("<")+":");
	    System.out.println(":"+contenido1.substring(0,52)+":");
	    
//	    DocumentBuilderFactory domFactory = 
//	        DocumentBuilderFactory.newInstance();
//	              domFactory.setNamespaceAware(true); 
//	        DocumentBuilder builder = domFactory.newDocumentBuilder();
//
//	        InputStream is = new ByteArrayInputStream(contenido.toString().getBytes("UTF-8"));	        
//	        
//	        Document doc = builder.parse(is);
//
//	        XPath xpath = XPathFactory.newInstance().newXPath();
//
//	        XPathExpression expr = xpath.compile("//td/strong/text()");
//
//	        Object result = expr.evaluate(doc, XPathConstants.NODESET);
//	        NodeList nodes = (NodeList) result;
//	        StringBuffer respuesta = new StringBuffer();
//	        for (int i = 0; i < nodes.getLength(); i++) {
//	        	respuesta.append(nodes.item(i).getNodeValue());
//	        	break;
//	        }
//	        String miout = respuesta.toString().trim();
//	        System.out.println(";"+contenido+";");

	}

}
